package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.InspectionTemplateDto;
import com.be.beadmin.quality.dto.input.InspectionTotal;
import com.be.beadmin.quality.dto.output.InspectionTemplateEx;
import com.be.beadmin.quality.service.InspectionTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
* @author ljh
* @date 2023-05-18
**/
@Api(tags = {"检验表模板"})
@RestController
public class InspectionTemplateController {

    @Autowired
    private  InspectionTemplateService inspectionTemplateService;

    @ApiOperation("判重:过程检验+[柜体类型]+[工序名称]，")
    @PostMapping(value = "/quality/checkInspectionTemplateByProductAndProcess")
    public ResultModel checkInspectionTemplateByProductAndProcess(
            @ApiParam(value = "柜体类型",required = true) @RequestParam String productType,
            @ApiParam(value = "工序名称",required = true) @RequestParam String processName
    )  {
        try {
            return ResultModel.success(inspectionTemplateService.checkInspectionTemplateByProductAndProcess(productType,processName));
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation("(柜体类型+模板类型)获取检验表模板")
    @PostMapping(value = "/quality/getInspectionTemplateByProductTypeAndType")
    public ResultModel getInspectionTemplateByProductTypeAndType(
            @ApiParam(value = "模板类型",required = true) @RequestParam String type,
            @ApiParam(value = "柜体类型",required = true) @RequestParam String productType
    )  {
        try {
            InspectionTotal inspectionTotal = inspectionTemplateService.getInspectionTemplateByProductTypeAndType(type,productType);
            return ResultModel.success(inspectionTotal) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation("名称模糊查询：(柜体类型+模板类型+名称（接线过程检验/装配过程检验/检验过程检验）模糊查询)获取检验表模板")
    @PostMapping(value = "/quality/getInspectionTemplateByProductTypeAndTypeAndName")
    public ResultModel getInspectionTemplateByProductTypeAndTypeAndName(
            @ApiParam(value = "模板类型",required = true) @RequestParam String type,
            @ApiParam(value = "柜体类型",required = true) @RequestParam String productType,
            @ApiParam(value = "检验表名称",required = true) @RequestParam String name
    )  {
//        try {
            InspectionTotal inspectionTotal = inspectionTemplateService.getInspectionTemplateByProductTypeAndTypeAndName(type,productType,name);
            return ResultModel.success(inspectionTotal);
//        } catch (BeadminException e) {
//            return ResultModel.error(-1,e.getMessage());
//        } catch (Exception e) {
//            return ResultModel.error(-2,e.getMessage());
//        }
    }


    @ApiOperation("获取检验表类型")
    @PostMapping(value = "/quality/getInspectionType")
    public ResultModel getInspectionType()  {
        List<String> list = inspectionTemplateService.getInspectionType();
        return ResultModel.success(list) ;
    }

    @ApiOperation("发布&取消发布")
    @PostMapping(value = "/quality/publishInspectionTemplate")
    public ResultModel publishInspectionTemplate(@ApiParam(value = "检验表模板Id") @RequestParam String inspectionTemplateId,
                                                 @ApiParam(value = "发布时间") @RequestParam Date publishTime)  {
        inspectionTemplateService.publishInspectionTemplate(inspectionTemplateId,publishTime);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("检验表模板分页信息")
    @PostMapping(value = "/quality/getInspectionTemplateByPage")
    public ResultModel getInspectionTemplateByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectionTemplateService.getInspectionTemplateByPage(pageEntity)) ;
    }

    @ApiOperation("（判断名称唯一）根据模板名称获取检验表模板")
    @PostMapping("/quality/getInspectionTemplateByNameAndProductType")
    public ResultModel getInspectionTemplateByNameAndProductType(
            @ApiParam(value = "模板名称",required = true) @RequestParam String name,
            @ApiParam(value = "柜体类型",required = true) @RequestParam String productType){
        try {
            return ResultModel.success(inspectionTemplateService.getInspectionTemplateByNameAndProductType(name,productType));
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }

    }

    @ApiOperation("新建检验表模板")
    @PostMapping("/quality/addInspectionTemplate")
    public ResultModel addInspectionTemplate(@RequestBody InspectionTemplateDto inspectionTemplateDto){
        inspectionTemplateService.addInspectionTemplate(inspectionTemplateDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("删除检验表模板")
    @PostMapping("/quality/delInspectionTemplate")
    public ResultModel delInspectionTemplate(@RequestBody InspectionTemplateDto inspectionTemplateDto){
        inspectionTemplateService.delInspectionTemplate(inspectionTemplateDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("修改检验表模板")
    @PostMapping("/quality/updateInspectionTemplate")
    public ResultModel updateInspectionTemplate(@RequestBody InspectionTemplateDto inspectionTemplateDto){
        inspectionTemplateService.updateInspectionTemplate(inspectionTemplateDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportInspectionTemplate")
    public void exportInspectionTemplate(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectionTemplateEx> inspectionTemplatePage = inspectionTemplateService.getInspectionTemplateByPage(pageEntity);
        List<InspectionTemplateEx> inspectionTemplateExes = inspectionTemplatePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectionTemplate接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), InspectionTemplateEx.class).sheet("sheet1").doWrite(inspectionTemplateExes);
    }
}